import {scrollMixin} from "@/utils/mixin";
import yzBlank from "@/components/ui_components/yz_blank";
export default{
  mixins:[scrollMixin],
  data(){
    return{
      listData: [],
      isLoadMore:true,
      page:1,
      last_page:1
    }
  },
  activated() {
    let myLocation = JSON.parse(localStorage.getItem("myLocation"))
    if(myLocation){
      this.myLocation = myLocation;
    }
    this.lawyerId = this.$route.params.id
    if(this.$route.name=='lawyerSearch'){
      this.lawyerId=this.$route.query.id;
    }
    if(this.request == false){
      return false;
    }
    if(this.httpUrl){
      this.getdata();
    }
  },
  methods: {
    initdata(){
      this.page = 1;
      this.listData = [];
    },
    getdata(searchName="") {
      this.initdata();
      let json = {}
      if(this.lawyerId && this.$route.name !== 'lawyerSearch'){
        json.id = this.lawyerId
      }
      if(searchName){
        json = {
          id : this.lawyerId,
          name:searchName
        }
      }
      if(this.$route.name == 'lawyerSearch'){
        if(this.text !== '距离'){
          console.log("this.myLocation.point",this.myLocation.point);
          json.distance_type = this.text == "由近到远" ? 1 : 2;
        }
        if(this.myLocation && this.myLocation.point && this.myLocation.point.lng &&  this.myLocation.point.lat){
          json.lng  = this.myLocation.point.lng;
          json.lat  = this.myLocation.point.lat;
        }
        json.category_id = this.lawyerId
      }
      if(this.$route.name == 'lawyerConsult'){
        json.goods_type = this.$route.params.consult_type
      }
      // if (this.myLocation.point) {

      $http.post(this.httpUrl, json, "正在加载中...").then(({ result, msg, data }) => {
        if (result) {
          this.listData = data.list.data;
          this.last_page = data.list.last_page;
        }else this.$toast(msg);
      })
    },
    getMoreData(){
      this.isLoadMore = false;
      if(this.page>=this.last_page) return;
      let page = this.page + 1;
      let json = {
        page,
      }
      if(this.$route.name == 'lawyerConsult'){
        json.goods_type = this.$route.params.consult_type
      }
      $http.post(this.httpUrl, json, "正在加载更多数据中...").then(({ result, msg, data }) => {
        this.isLoadMore = true;
        if (result) {
          this.page = page;
          this.listData = this.listData.concat(data.list.data);
        }else this.$toast(msg);
      })
    }
  },
  watch:{
    search(value){
      if(this.timerId){
      clearTimeout(this.timerId);}
      this.timerId = setTimeout(() => {
        this.getdata(value)
      }, 300);
    },
    lawyers:function(data){
      if(data && data.length>0){
        this.listData = data;
      }
    },
    immediate:true,
  },
  components:{yzBlank}
}